<?php

namespace App\Admin\Http\Controllers\Web;

use Flash;
use App\Admin\Eloquent\Navigation;
use App\Admin\Http\Requests\NavigationRequest;
use Illuminate\Http\Request;

class NavigationsController extends \App\Admin\Http\Controllers\Controller
{
    /**
     * 查询导航列表页
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $query = $request->only('name', 'parent_id');

        $parents = Navigation::getParents();

        $navigations =Navigation::paging_query($query,15);

        $links = $navigations->render();

        return view('admin.web.navigations.index', compact('query', 'parents', 'navigations', 'links', 'query'));
    }

    /**
     * 跳转到新增导航表单
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $parents = Navigation::getParents();

        return view('admin.web.navigations.create', compact('parents'));
    }

    /**
     * 保存一条导航记录
     *
     * @param NavigationRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(NavigationRequest $request)
    {
        $navigation = new Navigation($request->all());

        $navigation->save();

        Flash::success('新增导航信息成功!');

        return $this->redirectAction('Web\NavigationsController@index');
    }

    /**
     * 返回修改导航表单页面
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(int $id){
        //查询父级导航类型
        $parents = Navigation::getParents($id);

        //查询id对应导航记录,如果该记录不存在,抛出异常
        $navigation = Navigation::findOrFail($id);

        return view('admin.web.navigations.edit',compact('parents','navigation'));
    }

    /**
     * 修改一条导航记录
     *
     * @param NavigationRequest $request
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(NavigationRequest $request,int $id){
        $args =$request->all();

        $navigation = Navigation::findOrFail($id);

        $navigation->update($args);

        Flash::success('修改导航信息成功!');

        return $this->redirectAction('Web\NavigationsController@index');
    }

    /**
     *  展示导航详情
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(int $id){
        $navigation = Navigation::leftJoin('navigations as a', 'a.id', '=', 'navigations.parent_id')
            ->select('navigations.description','navigations.id', 'navigations.name', 'a.name as parent_name', 'navigations.sort','navigations.link')
            ->where('navigations.id',$id)
            ->first();

        return view('admin.web.navigations.show',compact('navigation'));
    }

    /**
     * 根据导航id删除导航记录
     *
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(int $id){
        $navigation = Navigation::findOrFail($id);

        $navigation->delete();

        Flash::success('删除导航记录成功!');

        return $this->redirectAction('Web\NavigationsController@index');
    }
}